package com.qs.home.ui.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qs.base.adapter.ViewPagerAdaper;
import com.qs.base.base.BaseMyActivity;
import com.qs.base.global.SPKeyGlobal;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.router.RouterFragmentPath;
import com.qs.base.utils.BaseTimeUtils;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeIdentifyDetailsBinding;
import com.qs.home.entity.TextResultEntity;
import com.qs.home.ui.VM.IdentifyDetailsViewModel;
import com.qs.home.ui.VM.IdentifyResultsItemViewModel;
import com.qs.home.ui.fragment.TranslateFragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 识别详情
 *
 * @Author ltzz
 * @Date 2019/8/20
 */
@Route(path = RouterActivityPath.Home.IDENTIFY_DETAILS)
public class IdentifyDetailsActivity extends BaseMyActivity<HomeIdentifyDetailsBinding, IdentifyDetailsViewModel> implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private FragmentManager mFragmentManager;

    private ViewPagerAdaper viewPagerAdaper;
    List<Fragment> mFragmentList = new ArrayList<Fragment>();

    //识别结果数据
    @Autowired()
    String textResult;

    @Override
    protected boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected View titleLayout() {
        return binding.head;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.home_identify_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String result = "";
        if (!StringUtils.isTrimEmpty(textResult)) {
            Gson gson = new Gson();
            try {
                TextResultEntity textResultEn = gson.fromJson(textResult, TextResultEntity.class);
                viewModel.textResultOb.set(textResultEn);

                if (textResultEn.getWords_result() != null && textResultEn.getWords_result().size() > 0) {
                    for (int i = 0; i < textResultEn.getWords_result().size(); i++) {
                        if (i == 0)
                            result = textResultEn.getWords_result().get(i).getWords();
                        else
                            result = result + textResultEn.getWords_result().get(i).getWords();
                    }
                    viewModel.textResultStr.set(result);
                }
            } catch (Exception e) {

            }
        }

        mFragmentManager = getSupportFragmentManager();
        mFragmentList.add((Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.ORIGINAL_FRAGMENT).withParcelable("textResult", (Parcelable) viewModel.textResultOb.get()).navigation());
        mFragmentList.add((Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.TEXT_FRAGMENT).withParcelable("textResult", (Parcelable) viewModel.textResultOb.get()).navigation());
        mFragmentList.add((Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.TRANSLATE_FRAGMENT).withString("textResult", result).navigation());

        viewPagerAdaper = new ViewPagerAdaper(mFragmentManager, mFragmentList);

        binding.original.setOnClickListener(this);
        binding.character.setOnClickListener(this);
        binding.translate.setOnClickListener(this);

        initViewPager();
    }

    public void initViewPager() {
        binding.viewPg.addOnPageChangeListener(this);
        binding.viewPg.setAdapter(viewPagerAdaper);
        binding.viewPg.setCurrentItem(0);
        binding.viewPg.setOffscreenPageLimit(1);
        setChangText(true, false, false);
    }

    private void setChangText(boolean o, boolean two, boolean thre) {
        binding.original.setSelected(o);
        binding.originalLine.setVisibility(o ? View.VISIBLE : View.GONE);
        binding.character.setSelected(two);
        binding.characterLine.setVisibility(two ? View.VISIBLE : View.GONE);
        binding.translate.setSelected(thre);
        binding.translateLine.setVisibility(thre ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.original) {
            //原图
            setChangText(true, false, false);
            binding.viewPg.setCurrentItem(0);
        }
        if (id == R.id.character) {
            //文字
            setChangText(false, true, false);
            binding.viewPg.setCurrentItem(1);
        }
        if (id == R.id.translate) {
            //翻译
            setChangText(false, false, true);
            binding.viewPg.setCurrentItem(2);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                setChangText(true, false, false);
                break;
            case 1:
                //是否会员
                boolean isOk = false;
                if (!StringUtils.isTrimEmpty(SPUtils.getInstance().getString(SPKeyGlobal.USER_ID))) {
                    //用户已登录
                    //查看是否是会员
                    String date = SPUtils.getInstance().getString(SPKeyGlobal.USER_DATE, "");
                    if (!StringUtils.isTrimEmpty(date)) {
                        if (Float.parseFloat(date) > 0) {
                            isOk = true;
                        }
                    }
                }
                String seeNumberStr = SPUtils.getInstance().getString(SPKeyGlobal.SEE_NUMBER, "0");
                float x = Float.parseFloat(seeNumberStr);
                if (!isOk) {
                    if (x <= 0) {
                        binding.viewPg.setCurrentItem(0);
                        ToastUtils.showShort("识别次数已用完");
                        return;
                    }
                }
                setChangText(false, true, false);
                break;
            case 2:
                setChangText(false, false, true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //切换翻译
    public void pageChangedTranslate(String str) {
        setChangText(false, false, true);
        if (!StringUtils.isTrimEmpty(str)) {
            TranslateFragment translateFragment = (TranslateFragment) mFragmentList.get(2);
            translateFragment.setTranslate(str);
        }
        binding.viewPg.setCurrentItem(2);
    }
}
